package com.example.chatroom.service.impl;

import com.example.chatroom.common.errorcode.ServiceErrorCodeConstants;
import com.example.chatroom.common.exception.ServiceException;
import com.example.chatroom.controller.param.AddApplyFriendParam;
import com.example.chatroom.controller.param.GetUsersParam;
import com.example.chatroom.controller.param.HandlerApplyFriendParam;
import com.example.chatroom.controller.result.GetApplyFriendsResult;
import com.example.chatroom.dao.dataobject.ApplyFriendDO;
import com.example.chatroom.dao.dataobject.FriendDO;
import com.example.chatroom.dao.dataobject.FriendInfoDO;
import com.example.chatroom.dao.dataobject.UserDO;
import com.example.chatroom.dao.mapper.ApplyFriendMapper;
import com.example.chatroom.dao.mapper.FriendMapper;
import com.example.chatroom.dao.mapper.UserMapper;
import com.example.chatroom.service.IApplyFriendService;
import com.example.chatroom.service.dto.GetApplyFriendsDTO;
import com.example.chatroom.service.dto.GetUsersDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ApplyFriendServiceImpl implements IApplyFriendService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ApplyFriendMapper applyFriendMapper;
    @Autowired
    private FriendMapper friendMapper;
    @Override
    public GetUsersDTO getUsers(GetUsersParam getUsersParam,Long userId) {
        //1-    模糊查询
        List<String> userNames=userMapper.selectUserNameByStr(getUsersParam.getIndistinctName(),userId);
        GetUsersDTO getUsersDTO=new GetUsersDTO();
        getUsersDTO.setUserInfoList(userNames.stream().map(userName -> {
            UserDO userDO = userMapper.selectByUserName(userName);
            GetUsersDTO.UserInfo userInfo = new GetUsersDTO.UserInfo();
            userInfo.setUserId(userDO.getId());
            userInfo.setUserName(userName);
            return userInfo;
        }).collect(Collectors.toList()));
        return getUsersDTO;
    }

    @Override
    public Long addInfo(AddApplyFriendParam addApplyFriendParam) {
        //1-    如果已经是好友就不需要添加
        int count=friendMapper.selectByFT(addApplyFriendParam.getFormId(),addApplyFriendParam.getToId());
        if(count!=0){
            //说明是好友了
            log.info("addInfo 已经是好友了无需添加");
            return -1L;
        }
        ApplyFriendDO applyFriendDO=new ApplyFriendDO();
        applyFriendDO.setApplyContent(addApplyFriendParam.getApplyContent());
        applyFriendDO.setFormId(addApplyFriendParam.getFormId());
        applyFriendDO.setToId(addApplyFriendParam.getToId());
        applyFriendDO.setState(0);
        applyFriendMapper.insert(applyFriendDO);
        return applyFriendDO.getId();
    }

    @Override
    public List<GetApplyFriendsDTO> getApplyFriends(Long userId) {

        List<ApplyFriendDO>applyFriendDOS=applyFriendMapper.selectInfoBytoId(userId);
        return applyFriendDOS.stream().map(applyFriendDO -> {
            GetApplyFriendsDTO getApplyFriendsDTO=new GetApplyFriendsDTO();
            getApplyFriendsDTO.setGmtCreate(applyFriendDO.getGmtCreate());
            getApplyFriendsDTO.setApplyContent(applyFriendDO.getApplyContent());
            getApplyFriendsDTO.setFormName(userMapper.selectByPrimaryKey(applyFriendDO.getFormId()).getUserName());
            getApplyFriendsDTO.setFormId(applyFriendDO.getFormId());
            getApplyFriendsDTO.setToId(applyFriendDO.getToId());
            getApplyFriendsDTO.setState(applyFriendDO.getState());
            getApplyFriendsDTO.setId(applyFriendDO.getId());
            return getApplyFriendsDTO;
        }).collect(Collectors.toList());
    }

    @Transactional
    @Override
    public void handlerApplyFriend(HandlerApplyFriendParam handlerApplyFriendParam) {
        ApplyFriendDO applyFriendDO = applyFriendMapper.selectById(handlerApplyFriendParam.getId());
        if(applyFriendDO==null){
            log.error(ServiceErrorCodeConstants.APPLY_FRIEND_INFO_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.APPLY_FRIEND_INFO_ERROR);
        }
        //1-    校验是否已经是好友了
        if(friendMapper.selectByFT(applyFriendDO.getToId(), applyFriendDO.getFormId())!=0){
            //已经是好友了
            log.error(ServiceErrorCodeConstants.APPLY_FRIEND_FRIEND_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.APPLY_FRIEND_FRIEND_ERROR);
        }

        //2-    state是否为0
        if(applyFriendDO.getState()!=0){
            log.error(ServiceErrorCodeConstants.APPLY_FRIEND_STATE_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.APPLY_FRIEND_STATE_ERROR);
        }
        //3-    更新state
        if(handlerApplyFriendParam.getState()==0){
            log.error(ServiceErrorCodeConstants.APPLY_FRIEND_STATE.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.APPLY_FRIEND_STATE);
        }
        applyFriendMapper.updateState(handlerApplyFriendParam.getId(),handlerApplyFriendParam.getState());


        //4-    将记录插入friend表 需要添加两条记录
        if(handlerApplyFriendParam.getState()==1){
            FriendDO friendDO=new FriendDO();
            friendDO.setUserId(applyFriendDO.getFormId());
            friendDO.setFriendId(applyFriendDO.getToId());
            friendMapper.insertSelective(friendDO);
            FriendDO friendDO1=new FriendDO();
            friendDO1.setUserId(applyFriendDO.getToId());
            friendDO1.setFriendId(applyFriendDO.getFormId());
            friendMapper.insertSelective(friendDO1);
        }
    }
}
